package commands;

import indexes.BattingIndex;
import indexes.PlayerID;
import indexes.PlayerIDIndex;
import indexes.PlayerName;
import indexes.PlayerNameIndex;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import util.BattingDatabaseFile;
import util.DatabaseFile;
import util.Player;
import util.PlayerLogger;

import main.PlayerDB;

/**
 * The remove command deletes an entry for the given
 * PlayerID.  This requires removal from the db file,
 * the playerId index, and the playerName index.
 * @author bfults (Brian Fults - 905084698)
 *
 */
public class RemoveCommand extends Command
{

    
    /**
     * Constructor.
     * @param args
     */
    public RemoveCommand(List<String> args)
    {
        super(args);
    }
    
   

    @Override
    public boolean execute(
        PlayerIDIndex idIndex,
        PlayerNameIndex nameIndex,
        PlayerLogger logger,
        DatabaseFile database, BattingIndex battingIndex, BattingDatabaseFile battingDatabase ) throws IOException
    {
        logger.write(super.getDivider() + "\n");
        logger.write("Command: remove " + getArguments().get(0) + "\n");
        Long offsetToRemove = idIndex.get(getArguments().get(0));
        logger.write("with offset " + offsetToRemove + "\n");
        Player playerToRemove = database.getPlayer(offsetToRemove);
        
        idIndex.remove(getArguments().get(0));
        logger.write("Removed player ID index entry.\n");
        nameIndex.remove(new PlayerName(playerToRemove.getName(), offsetToRemove));
        logger.write("Removed player name index entry.\n");
        battingIndex.remove(getArguments().get(0));
        logger.write("Removed batting index entry.\n");
        return true;
    }
    
   


}
